package org.infinity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Util {

	private static final Log log = LogFactory.getLog(Util.class);

	private static ResourceBundle props = ResourceBundle
			.getBundle("filemonitor");

	/** file.separator */
	public static final String FILE_SEPARATOR = File.separator;

	/** dot */
	public static final String DOT = ".";

	/**
	 * save file 's last savetime
	 * 
	 * @param file
	 */
	static void saveFileTimestamp(File file) {
	}

	/**
	 * save a object to file
	 * 
	 * @param name
	 * @param object
	 */
	static void saveObject(String name, Object object) {
		log.debug("saveObject   start");
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(new File(name)));
			oos.writeObject(object);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			log.error(e);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e);
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
				log.error(e);
			}
		}
		log.debug("saveObject   end");
	}

	/**
	 * save a object to file
	 * 
	 * @param name
	 * @param object
	 */
	static void saveObject(Integer name, Object object) {
		// log.debug("saveObject start");
		saveObject(name.toString(), object);
		// log.debug("saveObject end");
	}

	/**
	 * load a object from file
	 * 
	 * @param name
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	static Object loadObject(String name) throws FileNotFoundException,
			IOException {
		log.debug("loadObject   start");
		Object obj = null;
		ObjectInputStream oos = null;
		try {
			oos = new ObjectInputStream(new FileInputStream(new File(name)));
			obj = oos.readObject();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			log.error(e);
		}finally{
			oos.close();
		}
		log.debug("loadObject   end");
		return obj;
	}

	/**
	 * Iterator all file of sub director
	 */
	// static void iteratorFile(File file) {
	// if (!file.isDirectory()) {
	// showMessage("warn.001");
	// return;
	// }
	// File[] files = file.listFiles();
	// for (File subFile : files) {
	// // subFile.
	// }
	// }
	/**
	 * getMessage
	 * 
	 * @param key
	 * @return
	 */
	public static String getMessage(String key) {
		return props.getString(key);
	}

	/**
	 * getMessage
	 * 
	 * @param key
	 * @return
	 */
	public static String getMessage(String key, Object... arg) {
		String message = getMessage(key);
		for (int i = 0; i < arg.length; i++) {
			Object object = arg[i];
			message = message.replaceFirst("\\{" + i + "\\}", Matcher
					.quoteReplacement(object.toString()));
		}
		return message;
	}

	/**
	 * showMessage
	 * 
	 * @param str
	 */
	static void showMessage(String key) {
		showMessage0(getMessage(key));
	}

	/**
	 * showMessage
	 * 
	 * @param str
	 */
	static void showMessage(String key, Object... args) {
		showMessage0(getMessage(key, args));
	}

	/**
	 * showMessage
	 * 
	 * @param str
	 */
	static void showMessage0(String message) {
		log.info(message);
		System.out.println(message);
		System.out
				.println("##########################################################");
	}

	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss.SSS");

	static String formatDate(Date date) {
		return sdf.format(date);
	}

	static String encode(String str) {
		String val = str.replaceAll(Matcher.quoteReplacement(FILE_SEPARATOR),
				DOT);
		val = val.replaceAll(":", "_.._");
		return val;
	}

	static String decode(String str) {
		String val = str;
		val = val.replaceAll("_\\.\\._", ":");
		val = val.replaceAll("\\" + DOT, Matcher
				.quoteReplacement(FILE_SEPARATOR));
		return val;
	}

	public static boolean checkPath(String destPath) {
		return checkPath(new File(destPath));
	}

	public static boolean checkPath(File destPath) {
		if (destPath.isDirectory()) {
			return true;
		} else {
			Util.showMessage("warn.001", destPath.getAbsoluteFile());
			return false;
		}
	}
}
